package com.slz3.business.module.memberManagement.drawTicket.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.drawTicket.domain.DrawTicket;
import com.slz3.business.module.memberManagement.drawTicket.domain.DrawTicketFactory;
import com.slz3.business.module.memberManagement.drawTicket.steward.DrawTicketSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class DrawTicketServiceImpl implements DrawTicketService {
    @Override
    public DrawTicket save(DrawTicket drawTicket) {
        return Repositorys.drawTicket.save(DrawTicketFactory.initBaseFields(drawTicket));
    }
    @Override
    public Page<DrawTicket> page(MultiValueMap<String, String> params) {
        Page<DrawTicket> drawTicketPage = Repositorys.drawTicket.findAll(
                DrawTicketSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return drawTicketPage;
    }


    @Override
    public DrawTicket modify(DrawTicket drawTicketNew) {
        return Repositorys.drawTicket.saveAndFlush(DrawTicketFactory.initBaseFields(drawTicketNew));
    }

    @Override
    public Optional<DrawTicket> single(UUID uuid) {
        return Repositorys.drawTicket.findById(uuid);
    }

    @Override
    public List<DrawTicket> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.drawTicket.findAllById(uuids);
    }

    @Override
    public List<DrawTicket> delete(List<DrawTicket> drawTicketList) {
        drawTicketList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            DrawTicketFactory.initBaseFields(item);
        });
        return Services.drawTicketTransactional.batchModify(drawTicketList);
    }

}
